/**
 * 
 */
package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

/**
 * @author A.Yerzhanov
 * Feb 19, 2013
 */
public class Mhrmintercheckoffline extends X_hrm_intercheckoffline {
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmintercheckoffline.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 602130550337925442L;


	/**
	 * @param ctx
	 */
	public Mhrmintercheckoffline(Properties ctx) {
		super(ctx);
	}
	
	/**
	 * @param ctx
	 * @param hrm_intercheckoffline_ID
	 * @param trxName
	 */
	public Mhrmintercheckoffline(Properties ctx, int hrm_intercheckoffline_ID,
			String trxName) {
		super(ctx, hrm_intercheckoffline_ID, trxName);
	}

	
	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public Mhrmintercheckoffline(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}

	/**
	 * Search inter check off line
	 * 
	 * @param C_BPartner_ID
	 * @param hrm_intercheckoff_ID
	 * @return  Model inter check off line
	 */
	public static Mhrmintercheckoffline getInterline(int C_BPartner_ID, int hrm_intercheckoff_id) {

		if (C_BPartner_ID == 0)
			return null;
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmintercheckoffline result = null;
			
		// 
		String sql_ = " SELECT * FROM HRM_INTERCHECKOFFLINE \n " +
						" WHERE ISACTIVE='Y' \n " +
						" AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
						" AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
						" AND HRM_INTERCHECKOFF_ID=" + hrm_intercheckoff_id;						
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new Mhrmintercheckoffline (Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
	return result;
	
	}		
	
	/* 
	 */
	@Override
	protected boolean beforeDelete() {

		Mhrmintercheckoff checkOff = new Mhrmintercheckoff(getCtx(),  gethrm_intercheckoff_ID(), get_TrxName());
		checkOff.sethrm_total(checkOff.gethrm_total().add(gethrm_amount().negate()));
		checkOff.saveEx();
		
		return super.beforeDelete();
	}

}
